<template>
  <el-popover :placement="placement" trigger="click" width="400">
    <el-tree
      ref="popupTree"
      class="tree"
      :data="data"
      :props="props"
      lazy
      :load="load"
      :node-key="nodeKey"
      :default-checked-keys="defaultCheckedKey"
      :highlight-current="true"
      :expand-on-click-node="true"
      @current-change="currentChangeHandle"
    />
    <template #reference>
      <el-input v-model="prop" readonly :placeholder="placeholder" style="cursor: pointer" />
    </template>
  </el-popover>
</template>

<script>
import { getChildrenMenu } from '../../api/menu'
// import { isEmpty } from 'lodash'
export default {
  name: 'MenuTree',
  props: {
    data: {
      type: Array,
      required: true,
    },
    props: {
      type: Object,
      required: true,
    },
    prop: {
      type: String,
      default: '',
    },
    nodeKey: {
      type: String || Number,
      required: true,
    },
    placeholder: {
      type: String,
      default: '点击选择内容',
    },
    placement: {
      type: String,
      default: 'right-start',
    },
    defaultExpandAll: {
      type: Boolean,
      default: false,
    },
    currentChangeHandle: {
      type: Function,
      default: null,
    },
    defaultKey: {
      type: String,
      default: '',
    },
    defaultCheckedKey: {
      type: Array,
      default() {
        return []
      },
    },
  },
  // data(){
  //   return{
  //     prop:''
  //   }
  // },
  // watch: {
  //   nodeKey: {
  //     handler(newName, oldName) {
  //       this.prop = this.nodeName(this.data)
  //     },
  //     immediate: true,
  //   },
  // },
  // mounted(){
  //   if(isEmpty(this.nodeKey) || this.nodeKey==0){
  //     this.prop=this.$t('message.menu.root')
  //   }else{
  //     this.prop=this.nodeName(this.data)
  //   }
  // },

  methods: {
    load(node, resolve) {
      if (node.level === 0) return resolve(this.data)
      if (node.level === 1) {
        return resolve(this.data[0].children)
      }
      getChildrenMenu(node.data.menuId).then(({ data }) => {
        resolve(data.data)
      })
    },
    // nodeName(popupTreeData) {
    //     popupTreeData.map(item=> {
    //       if (item.parentId == this.nodeKey) {
    //           return item.parentName
    //       }
    //     })

    //     if (popupTreeData.children && popupTreeData.children.length) {
    //         this.nodeName(popupTreeData.children)
    //     }
    // }
  },
}
</script>

<style scoped>
.tree {
  max-height: 200px;
  overflow: auto;
}
</style>
